package com.acoffee.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author acoffee
 * @create 2021-10-28 16:47
 */
public class MyBatisUtil {

    private static SqlSessionFactory sessionFactory;

    static {
        InputStream rs = null;
        try {
            rs = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }

         sessionFactory = new SqlSessionFactoryBuilder().build(rs);
    }

    public static SqlSession getSqlSession(){
        return sessionFactory.openSession();
    }

    public static void closeSqlSession(SqlSession sqlSession){
        sqlSession.close();
    }
}
